package main

import com.trolltech.qt.gui._
import com.trolltech.qt.core.Qt
import clark.model.QTreeItemBuilder
import model.Note
import clark.model.NoteTool
import org.apache.log4j.Logger
import java.text.SimpleDateFormat
import java.util.Date

/**
 * Created by IntelliJ IDEA.
 * Date: 2011-2-27
 * Time: 23:06:33
 * @author 崔钢
 * clarkhillm@gmail.com
 */

class LeftPanel(p: MainWindow) extends QDockWidget {
    setWindowTitle("笔记类型")

    val logger = Logger.getLogger(this.getClass.getSimpleName)

    def theCurrentItem(last: QTreeWidgetItem, item: QTreeWidgetItem): QTreeWidgetItem = {
        item.data(0, Qt.ItemDataRole.UserRole) match {
            case n: Note => logger.info("返回新的节点：" + item.data(0, Qt.ItemDataRole.UserRole))
            item
            case _ => logger.info("返回旧的节点：" + last.data(0, Qt.ItemDataRole.UserRole))
            last
        }
    }

    /**
     * 这个变量可以将偏函数拿出来
     */
    var currentI = (x: QTreeWidgetItem) => new QTreeWidgetItem

    var rootItem = QTreeItemBuilder.getRootItems

    val tree: QTreeWidget = new QTreeWidget(this) {
        setHeaderHidden(true)

        insertTopLevelItem(0, rootItem)

        itemExpanded.connect(this, "doSth()")
        itemCollapsed.connect(this, "doSth()")
        itemClicked.connect(this, "doSth()")

        def doSth {
            currentItem.data(0, Qt.ItemDataRole.UserRole) match {
                case "year" => QTreeItemBuilder.getMonth(currentItem)
                case "month" => QTreeItemBuilder.getDay(currentItem)
                case "day" => QTreeItemBuilder.getNote(currentItem)
                case n: Note => {
                    p.editor.setDocument(n.content)
                    p.currentNote = n
                    //logger.info("当前的TreeItem是：" + currentNote.data(0, Qt.ItemDataRole.UserRole))
                    currentI = theCurrentItem(currentItem, _: QTreeWidgetItem)
                }
                case _ => logger.warn("无子节点")
            }
        }
    }
    val tab = new QTabWidget(this) {
        setTabPosition(QTabWidget.TabPosition.South)
        addTab(tree, "日期")
    }

    def newNote(title: String) {
        if (title == "") {
            QMessageBox.warning(p, "警告", "您建立的笔记没有标题，请重建。")
        } else {
            val df = new SimpleDateFormat("HH:mm:ss")
            p.editor.setText(df.format(new Date) + " " + title)
            p.currentNote = new Note(-1, title, new Date, "", p.editor.document)
            saveNote
            p.currentNote = NoteTool.getLastNote

            if (rootItem.childCount>0) {
                rootItem = QTreeItemBuilder.getRootItems
            } else {
                tree.clear
                rootItem = QTreeItemBuilder.getRootItems
                tree.insertTopLevelItem(0, rootItem)
            }
        }
    }

    def itemData(item: QTreeWidgetItem): Object = {
        item.data(0, Qt.ItemDataRole.UserRole)
    }

    def saveNote() {
        val note = p.currentNote
        if (note.title != "") {
            logger.info("打算保存：" + note)
            NoteTool.save(note)
            logger.info("笔记" + note + "已保存")
        } else {
            logger.info("笔记" + note + "试图保存失败，没有标题")
        }
    }

    def deleteNote {
        val item = currentI(tree.currentItem)
        val parent = item.parent
        val note = item.data(0, Qt.ItemDataRole.UserRole).asInstanceOf[Note]
        logger.info("打算删除：" + note)
        NoteTool.removeNote(note.id)
        parent.removeChild(item)
        p.currentNote = p.emptyNote
        p.editor.setDocument(new QTextDocument)
    }

    setWidget(tab)
}